package com.nowcoder.community.controller.interceptor;

import com.nowcoder.community.entity.Message;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.MessageService;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Title: MessageInterceptor</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2022/8/22 10:14
 */

@Component
public class MessageInterceptor implements HandlerInterceptor {

    @Autowired
    private HostHolder hostHolder ;

    @Autowired
    private MessageService messageService ;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        User user = hostHolder.getUser() ;
        if(user != null && modelAndView != null){
            int letterUnreadCount = messageService.findLetterUnreadCount(user.getId() , null ) ;
            int noticeUnreadCount = messageService.findNoticeUnreadCount(user.getId() , null ) ;
            modelAndView.addObject("allUnreadCount" , letterUnreadCount + noticeUnreadCount) ;
        }
    }
}
